package com.parleys.android.client.util;

import android.net.Uri;

public class ParleysUriUtils {

	private static final String PARLEYS_PROXY_SCHEME			= "http";
	private static final String PARLEYS_PROXY_HOST				= "parleysproxy.appspot.com";
//	private static final String PARLEYS_PROXY_HOST				= "test.parleysproxy.appspot.com";
	private static final String PARLEYS_PROXY_BASE_URI			= "rest/v1";
	private static final String PARLEYS_PROXY_NEWS				= "news";
	private static final String PARLEYS_PROXY_SPACES			= "spaces";
	private static final String PARLEYS_PROXY_CHANNELS			= "channels";
	private static final String PARLEYS_PROXY_PRESENTATIONS		= "presentations";

	private static final String PARLEYS_ASSETS_SCHEME			= "http";
	private static final String PARLEYS_ASSETS_HOST				= "www.parleys.com";
	private static final String PARLEYS_ASSETS_BASE_URI			= "assets";
	private static final String PARLEYS_ASSETS_SPACES			= "spaces";
	private static final String PARLEYS_ASSETS_CHANNELS			= "channels";
	private static final String PARLEYS_ASSETS_PRESENTATIONS	= "presentations";
	
    private ParleysUriUtils () {}
	
	public static String buildProxyNewsUri(final long lastSync) {
		return baseProxyUriBuilder()
			.appendPath(PARLEYS_PROXY_NEWS)
			.appendPath("start")
			.appendPath(String.valueOf(lastSync)).build().toString();
			
	}
	
	public static String buildProxySpacesUri() {
		return baseProxyUriBuilder()
			.appendPath(PARLEYS_PROXY_SPACES)
			.build().toString();			
	}
	
	public static String buildProxyChannelsUri(long spaceId) {
		return baseProxyUriBuilder()
			.appendPath(PARLEYS_PROXY_SPACES)
			.appendPath(String.valueOf(spaceId))
			.appendPath(PARLEYS_PROXY_CHANNELS)
			.build().toString();			
	}
	
	public static String buildProxyPresentationsUri(long channelId) {
		return baseProxyUriBuilder()
			.appendPath(PARLEYS_PROXY_CHANNELS)
			.appendPath(String.valueOf(channelId))
			.appendPath(PARLEYS_PROXY_PRESENTATIONS)
			.build().toString();			
	}
	
	public static String buildSpaceThumbnailUri(final String spaceId, final String thumbnail) {
		return baseAssetsUriBuilder()
			.appendPath(PARLEYS_ASSETS_SPACES)
			.appendPath(spaceId)
			.appendPath(thumbnail)
			.build().toString();
	}
	
	public static String buildChannelThumbnailUri(final String channelId, final String thumbnail) {
		return baseAssetsUriBuilder()
			.appendPath(PARLEYS_ASSETS_CHANNELS)
			.appendPath(channelId)
			.appendPath(thumbnail)
			.build().toString();
	}
	
	public static String buildPresentationThumbnailUri(final String presentationId, final String thumbnail) {
		return baseAssetsUriBuilder()
			.appendPath(PARLEYS_ASSETS_PRESENTATIONS)
			.appendPath(presentationId)
			.appendPath(thumbnail)
			.build().toString();
	}
	
	public static String removeLeadingSlashes(String urlPart) {
		return (urlPart != null ? urlPart.replaceFirst("^/+", "") : null);
	}
	
	private static Uri.Builder baseProxyUriBuilder() {
		return new Uri.Builder()
		.scheme(PARLEYS_PROXY_SCHEME)
		.authority(PARLEYS_PROXY_HOST)
		.appendEncodedPath(PARLEYS_PROXY_BASE_URI);
	}
	
	private static Uri.Builder baseAssetsUriBuilder() {
		return new Uri.Builder()
		.scheme(PARLEYS_ASSETS_SCHEME)
		.authority(PARLEYS_ASSETS_HOST)
		.appendEncodedPath(PARLEYS_ASSETS_BASE_URI);
	}

}
